﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace PomTray
{
	public partial class Form1 : Form
	{
		private NotifyIcon trayIcon;
		private ContextMenu trayMenu;
		private System.Threading.Timer tmr;

		int _worktime = 25;
		int _resttime = 5;
		int _minutesLeft = 0;

		private NotificationDialog _dlg = null;

		public Form1()
		{
			InitializeComponent();

			trayMenu = new ContextMenu();
			trayMenu.MenuItems.Add(String.Format("Start 55/5", _worktime, _resttime), OnStart1);
			trayMenu.MenuItems.Add(String.Format("Start 25/5", _worktime, _resttime), OnStart2);
			//trayMenu.MenuItems.Add(String.Format("Start 5/5", _worktime, _resttime), OnStart3);
			trayMenu.MenuItems.Add("Clear", OnClear);
			trayMenu.MenuItems.Add("Exit", OnExit);

			trayIcon = new NotifyIcon();
			trayIcon.Text = "PomTray";

			trayIcon.ContextMenu = trayMenu;
			trayIcon.Visible = true;

			this._dlg = new NotificationDialog();

			/* create timer but don't start */
			tmr = new System.Threading.Timer(OnTimer, null,
				System.Threading.Timeout.Infinite,
				System.Threading.Timeout.Infinite);

			OnClear(null, null);
		}

		protected override void OnLoad(System.EventArgs e)
		{
			Visible = false;
			ShowInTaskbar = false;
			base.OnLoad(e);
		}

		void OnStart1(object sender, EventArgs e)
		{
			OnStart(55, 5);
		}

		void OnStart2(object sender, EventArgs e)
		{
			OnStart(25, 5);
		}

		void OnStart3(object sender, EventArgs e)
		{
			OnStart(5, 5);
		}

		private void OnStart(int worktime, int resttime)
		{
			this._worktime = worktime;
			this._resttime = resttime;

			/* start timer at 1 minute intervals */
			_minutesLeft = _worktime + _resttime;
			tmr.Change(1000 * 60, 1000 * 60);
			//tmr.Change(1000, 1000);

			UpdateIcon(_minutesLeft.ToString(), Color.White, false);

			if (this._dlg.Visible == true)
			{
				this._dlg.InvokeClose();
			}
		}

		void OnClear(object sender, EventArgs e)
		{
			tmr.Change(
				System.Threading.Timeout.Infinite,
				System.Threading.Timeout.Infinite);
			UpdateIcon("--", Color.Gray, false);
		}

		void OnTimer(object o)
		{
			_minutesLeft--;

			if (_minutesLeft == 0)
			{
				OnClear(null, null);
			}

			/* time to rest */
			else if (_minutesLeft <= _resttime)
			{
				UpdateIcon(_minutesLeft.ToString(), Color.Red, true);
			}

			else if (_resttime == 10 && (_minutesLeft <= 15 || _minutesLeft % 5 == 0))
			{
				UpdateIcon(_minutesLeft.ToString(), Color.White, false);
			}

			else if (_resttime == 5 && (_minutesLeft <= 10 || _minutesLeft % 5 == 0))
			{
				UpdateIcon(_minutesLeft.ToString(), Color.White, false);
			}

			if (_minutesLeft == _resttime)
			{
				System.Media.SystemSounds.Asterisk.Play();

				this._dlg.SetMessage("Finish Up / Take a Break");
				this._dlg.HideRestartButton();

				if (!this._dlg.Visible)
				{
					this._dlg.ShowDialog();
				}
			}

			else if (_minutesLeft == 0)
			{
				System.Media.SystemSounds.Exclamation.Play();

				if (this._dlg.Visible)
				{
					this._dlg.InvokeClose();
					Thread.Sleep(100);
				}

				this._dlg.SetMessage("Break's Over, Get Back to Work");
				this._dlg.ShowRestartButton(
					string.Format("{0}/{1}", this._worktime, this._resttime));

				if (this._dlg.ShowDialog() == DialogResult.Retry)
				{
					this.OnStart(this._worktime, this._resttime);
				}
			}
		}

		void UpdateIcon(String text, Color c, bool bold)
		{
			Brush brush = new SolidBrush(c);
			Bitmap bitmap = new Bitmap(16, 16);
			Graphics graphics = Graphics.FromImage(bitmap);
			Font font = new Font(FontFamily.GenericSansSerif, 10.0f, bold ? FontStyle.Bold : FontStyle.Regular);
			graphics.DrawString(text, font, brush, 0,0);
			IntPtr hIcon = bitmap.GetHicon();
			Icon icon = Icon.FromHandle(hIcon);
			trayIcon.Icon = icon;
		}

		void OnExit(object sender, EventArgs e)
		{
			trayIcon.Visible = false;
			Application.Exit();
		}
	}
}
